home *** CD-ROM | disk | FTP | other *** search
/ Teach Your Children: Road Construction Ahead / Teach Your Children: Road Construction Ahead.iso / pc / rca / road.dxr / 00093_SimHandlers.ls < prev    next >
Encoding:
Text File  |  1996-07-17  |  14.0 KB  |  420 lines

  1. global gSimObject, gTruckObject, gLevelObject, gGasGaugeObject, gCDpath, gRollAnimButton, gCulvertObject, gDebug, gFileSep
  2.  
  3. on checkBlockage adjCell, adjObj, adjVector
  4.   set blockedFlag to 0
  5.   set riverTileName to char 1 of the name of cast the tileNum of adjObj
  6.   set bridgeList to getProp(the bridgeList of gSimObject, riverTileName)
  7.   set bridgeData to getProp(bridgeList, adjVector)
  8.   set exitDir to getAt(bridgeData, 2)
  9.   set otherSideCell to adjCell + getProp(the moveList of gSimObject, exitDir)
  10.   set otherSideObj to getAt(the cellList of gSimObject, otherSideCell)
  11.   if getPos(the adjacentList of adjObj, exitDir) = 0 then
  12.     set blockedFlag to 1
  13.   end if
  14.   if (the tileType of otherSideObj = #Road) or (the tileType of otherSideObj = #Bridge) or (the tileType of otherSideObj = #Building) or (the tileType of otherSideObj = #immovable) or (the tileType of otherSideObj = #River) then
  15.     set blockedFlag to 1
  16.   end if
  17.   return blockedFlag
  18. end
  19.  
  20. on placeObstacle obstacleDataList
  21.   set options to (count(obstacleDataList) - 1) / 3
  22.   set probability to getLast(obstacleDataList)
  23.   repeat with cell = 1 to count(the cellList of gSimObject)
  24.     if getOne(the pathList of gSimObject, cell) = 0 then
  25.       set cellObj to getAt(the cellList of gSimObject, cell)
  26.       if the tileType of cellObj = #BG then
  27.         if random(100) <= probability then
  28.           set choice to (random(options) - 1) * 3
  29.           set the tileType of cellObj to getAt(obstacleDataList, choice + 1)
  30.           set firstTileNum to getAt(obstacleDataList, choice + 2)
  31.           set variations to getAt(obstacleDataList, choice + 3)
  32.           set the tileNum of cellObj to firstTileNum + random(variations) - 1
  33.           set the startTileType of cellObj to the tileType of cellObj
  34.           set the startTileNum of cellObj to the tileNum of cellObj
  35.         end if
  36.       end if
  37.     end if
  38.   end repeat
  39. end
  40.  
  41. on navButtonHandler
  42.   set simButList to the simButtonList of gSimObject
  43.   if the status of gTruckObject <> #animating then
  44.     if not objectp(gRollAnimButton) and (the available of getProp(simButList, #alert) <> 1) then
  45.       if buttonHandler() = 1 then
  46.         startBuffering()
  47.         puppetSound("arrowClick")
  48.         updateStage()
  49.         if the startSimFlag of gSimObject = 1 then
  50.           set the startSimFlag of gSimObject to 0
  51.         end if
  52.         set whichbutton to the clickOn - the channel of getProp(simButList, #n) + 1
  53.         set move to getPropAt(simButList, whichbutton)
  54.         repeat while soundBusy(1)
  55.         end repeat
  56.         puppetSound(0)
  57.         moveTruck(gTruckObject, move)
  58.         flushBuffer()
  59.       end if
  60.     end if
  61.   end if
  62. end
  63.  
  64. on refreshControls availableMoveList
  65.   repeat with x = 1 to 8
  66.     set position to getPos(availableMoveList, getAt(the arrowList of gSimObject, x))
  67.     set buttonObject to getAt(the simButtonList of gSimObject, x)
  68.     if position = 0 then
  69.       set the available of buttonObject to 0
  70.       set the locH of sprite the channel of buttonObject to 1000
  71.       next repeat
  72.     end if
  73.     set the available of buttonObject to 1
  74.     set the locH of sprite the channel of buttonObject to the locH of getAt(the simButtonList of gSimObject, x)
  75.   end repeat
  76. end
  77.  
  78. on resetGame
  79.   puppetSound("RESETSIM")
  80.   removeMessage()
  81.   showDemoButton()
  82.   set the startSimFlag of gSimObject to 1
  83.   set the finishedFlag of gSimObject to 0
  84.   set the demoFlag of gSimObject to 0
  85.   set the buildingsConnected of gSimObject to 0
  86.   hideInvisibleButtons(gSimObject)
  87.   set the invisibleButtonList of gSimObject to [0, 0, 0, 0, 0, 0, 0, 0]
  88.   erasePlayfield(gSimObject)
  89.   repeat with x = 1 to count(the cellList of gSimObject)
  90.     set cellObj to getAt(the cellList of gSimObject, x)
  91.     if the tileType of cellObj = #Road then
  92.       set the connector of cellObj to 0
  93.     else
  94.       if the tileType of cellObj = #Building then
  95.         set the connector of cellObj to 0
  96.       end if
  97.     end if
  98.     set the tileNum of cellObj to the startTileNum of cellObj
  99.     set the tileType of cellObj to the startTileType of cellObj
  100.     initAdjacentList(cellObj)
  101.   end repeat
  102.   initTruckProps(gTruckObject, the firstCell of gTruckObject, the startDirection of gTruckObject)
  103.   set availableMoveList to createAvailableList(gTruckObject, the cell of gTruckObject)
  104.   refreshControls(availableMoveList)
  105.   placeInvisibleButtons(gSimObject, availableMoveList, 0)
  106.   resetGasGauge(gGasGaugeObject, count(the pathList of gSimObject))
  107.   redrawBG(gSimObject)
  108.   drawTruck(gTruckObject)
  109.   repeat while soundBusy(1)
  110.   end repeat
  111.   puppetSound(0)
  112. end
  113.  
  114. on afterPlayback
  115.   set the mouseDownScript to EMPTY
  116.   showDemoButton()
  117.   hideInvisibleButtons(gSimObject)
  118.   set the invisibleButtonList of gSimObject to [0, 0, 0, 0, 0, 0, 0, 0]
  119.   erasePlayfield(gSimObject)
  120.   newGameAlert()
  121.   updateStage()
  122.   clearGrid()
  123.   newGame(gSimObject)
  124.   set availableMoveList to createAvailableList(gTruckObject, the cell of gTruckObject)
  125.   refreshControls(availableMoveList)
  126.   updateStage()
  127.   flushBuffer()
  128.   puppetSimButtons()
  129.   simIntroSong()
  130.   go(the frame)
  131. end
  132.  
  133. on clearGrid
  134.   repeat with x = 1 to count(the cellList of gSimObject)
  135.     set cellObj to getAt(the cellList of gSimObject, x)
  136.     if the tileType of cellObj = #Road then
  137.       set the connector of cellObj to 0
  138.     else
  139.       if the tileType of cellObj = #Building then
  140.         set the connector of cellObj to 0
  141.       end if
  142.     end if
  143.     set the startTileNum of cellObj to 0
  144.     set the tileNum of cellObj to 0.0
  145.     set the startTileType of cellObj to #BG
  146.     set the tileType of cellObj to #BG
  147.     initAdjacentList(cellObj)
  148.   end repeat
  149. end
  150.  
  151. on areYouStuck cell, availableMoveList
  152.   set stuckFlag to 1
  153.   if count(availableMoveList) > 0 then
  154.     repeat with adjVector in availableMoveList
  155.       set cellObj to getAt(the cellList of gSimObject, cell + getProp(the moveList of gSimObject, adjVector))
  156.       if the tileType of cellObj <> #immovable then
  157.         set stuckFlag to 0
  158.         exit repeat
  159.       end if
  160.     end repeat
  161.   end if
  162.   if stuckFlag = 1 then
  163.     stuckAlert()
  164.   end if
  165.   return stuckFlag
  166. end
  167.  
  168. on blockCrossings whatCell, latestMove
  169.   global n, NE, E, SE, s, SW, w, NW
  170.   set blockList to []
  171.   if (latestMove = #n) or (latestMove = #s) then
  172.     if latestMove = #n then
  173.       set blockList to blockCell(whatCell, blockList, #NW, #E)
  174.       set blockList to blockCell(whatCell, blockList, #NE, #w)
  175.     else
  176.       set blockList to blockCell(whatCell, blockList, #SW, #E)
  177.       set blockList to blockCell(whatCell, blockList, #SE, #w)
  178.     end if
  179.   else
  180.     if (latestMove = #E) or (latestMove = #w) then
  181.       if latestMove = #E then
  182.         set blockList to blockCell(whatCell, blockList, #SE, #n)
  183.         set blockList to blockCell(whatCell, blockList, #NE, #s)
  184.       else
  185.         set blockList to blockCell(whatCell, blockList, #SW, #n)
  186.         set blockList to blockCell(whatCell, blockList, #NW, #s)
  187.       end if
  188.     end if
  189.   end if
  190.   return blockList
  191. end
  192.  
  193. on blockCell whatCell, blockList, vector, blockedMove
  194.   if getOne(the adjacentList of getAt(the cellList of gSimObject, whatCell), vector) <> 0 then
  195.     set blockCell to whatCell + getProp(the moveList of gSimObject, vector)
  196.     set adjList to the adjacentList of getAt(the cellList of gSimObject, blockCell)
  197.     set position to getPos(adjList, blockedMove)
  198.     if position <> 0 then
  199.       deleteAt(adjList, position)
  200.     end if
  201.     add(blockList, blockCell)
  202.   end if
  203.   return blockList
  204. end
  205.  
  206. on checkForBridge spriteObj, cellObj
  207.   set the tileType of cellObj to the startTileType of cellObj
  208.   set the tileNum of cellObj to the startTileNum of cellObj
  209.   set reverseMove to getProp(the convertList of gSimObject, getLast(the recordedMoveList of gSimObject))
  210.   deleteAt(the recordedMoveList of gSimObject, count(the recordedMoveList of gSimObject))
  211.   set the cell of spriteObj to the cell of spriteObj + getProp(the moveList of gSimObject, reverseMove)
  212.   set the cellObject of spriteObj to getAt(the cellList of gSimObject, the cell of spriteObj)
  213.   set cellObj to the cellObject of spriteObj
  214.   set the Xloc of spriteObj to the Xloc of cellObj
  215.   set the Yloc of spriteObj to the Yloc of cellObj
  216.   if the tileType of cellObj = #Bridge then
  217.     checkForBridge(spriteObj, the cellObject of spriteObj)
  218.   end if
  219. end
  220.  
  221. on surroundSprite cellNum, newMove
  222.   set cellObj to getAt(the cellList of gSimObject, cellNum)
  223.   clearCoverTiles()
  224.   set coverList to getProp(getProp(the covList of gSimObject, the cellType of cellObj), newMove)
  225.   set cellCount to 1
  226.   repeat with spriteNum = the firstSurroundSprite of gSimObject to the firstSurroundSprite of gSimObject + count(coverList) - 1
  227.     set cellIndex to cellNum + getAt(coverList, cellCount)
  228.     if (cellIndex > 0) and (cellIndex <= count(the cellList of gSimObject)) then
  229.       set coverCell to getAt(the cellList of gSimObject, cellIndex)
  230.       redrawCell(coverCell, spriteNum)
  231.     end if
  232.     set cellCount to cellCount + 1
  233.   end repeat
  234.   updateStage()
  235. end
  236.  
  237. on interruptDemo
  238.   startBuffering()
  239.   if (marker(0) = label("River")) or (marker(0) = label("BridgeDone")) then
  240.     clearAllActors()
  241.     puppetPalette("RiverPal")
  242.     go("Grass")
  243.     go(the frame + 1)
  244.     repeat with button = 1 to 4
  245.       set the visible of sprite (the firstArrowChan of gCulvertObject + button - 1) to 1
  246.     end repeat
  247.     puppetPalette("SimPal", 60)
  248.     updateStage()
  249.     puppetPalette(0)
  250.     unLoadCast()
  251.   end if
  252.   unpuppetAll()
  253.   set the demoFlag of gSimObject to 0
  254.   set the finishedFlag of gSimObject to 1
  255.   set the obstacleState of gSimObject to EMPTY
  256.   set the programmedMove of gTruckObject to 0
  257.   set the status of gTruckObject to #idle
  258.   go(label("Simulator" & string(the level of gLevelObject)))
  259.   set the mouseDownScript to EMPTY
  260.   flushBuffer()
  261.   abort()
  262. end
  263.  
  264. on clearCoverTiles
  265.   repeat with spriteNum = the firstSurroundSprite of gSimObject to the firstSurroundSprite of gSimObject + 8 - 1
  266.     set the locH of sprite spriteNum to 1000
  267.   end repeat
  268. end
  269.  
  270. on puppetSurroundSprites
  271.   repeat with spriteNum = the firstSurroundSprite of gSimObject to the firstSurroundSprite of gSimObject + 6 - 1
  272.     puppetSprite(spriteNum, 1)
  273.   end repeat
  274. end
  275.  
  276. on unPuppetSurroundSprites
  277.   repeat with spriteNum = the firstSurroundSprite of gSimObject to the firstSurroundSprite of gSimObject + 6 - 1
  278.     puppetSprite(spriteNum, 0)
  279.   end repeat
  280. end
  281.  
  282. on unpuppetSimButtons
  283.   repeat with button in the simButtonList of gSimObject
  284.     puppetSprite(the channel of button, 0)
  285.   end repeat
  286.   repeat with x = 1 to count(the invisibleButtonList of gSimObject)
  287.     set channel to the firstInvisButChan of gSimObject + x - 1
  288.     puppetSprite(channel, 0)
  289.   end repeat
  290.   if the level of gLevelObject = 3 then
  291.     puppetSprite(the channel of gGasGaugeObject, 0)
  292.   end if
  293. end
  294.  
  295. on puppetSimButtons
  296.   repeat with button in the simButtonList of gSimObject
  297.     puppetSprite(the channel of button, 1)
  298.   end repeat
  299.   repeat with x = 1 to count(the invisibleButtonList of gSimObject)
  300.     set channel to the firstInvisButChan of gSimObject + x - 1
  301.     puppetSprite(channel, 1)
  302.   end repeat
  303. end
  304.  
  305. on hideControls
  306.   repeat with x = 1 to 8
  307.     set channel to the channel of getAt(the simButtonList of gSimObject, x)
  308.     set the locH of sprite channel to 1000
  309.   end repeat
  310. end
  311.  
  312. on hideDemoButton
  313.   set demoObject to getProp(the simButtonList of gSimObject, #Demo)
  314.   set the locH of sprite the channel of demoObject to 1000
  315.   set the available of demoObject to 0
  316. end
  317.  
  318. on showDemoButton
  319.   set demoObject to getProp(the simButtonList of gSimObject, #Demo)
  320.   set the locH of sprite the channel of demoObject to the locH of demoObject
  321.   set the locV of sprite the channel of demoObject to the locV of demoObject
  322.   set the castNum of sprite the channel of demoObject to the bmap of demoObject
  323.   set the available of demoObject to 1
  324. end
  325.  
  326. on unpuppetControls
  327.   repeat with x = 1 to count(the simButtonList of gSimObject)
  328.     set channel to the channel of getAt(the simButtonList of gSimObject, x)
  329.     puppetSprite(channel, 0)
  330.   end repeat
  331. end
  332.  
  333. on stuckAlert
  334.   startBuffering()
  335.   sound playFile 1, gCDpath & "SOUND" & gFileSep & "SIM" & gFileSep & "10VOSTUK"
  336.   set buildingsConnected to 0
  337.   repeat while soundBusy(1)
  338.   end repeat
  339.   resetGame()
  340.   flushBuffer()
  341. end
  342.  
  343. on outOfGasAlert
  344.   startBuffering()
  345.   set soundName to getAt(["10FUEL", "10FUEL2"], random(2))
  346.   sound playFile 1, gCDpath & "SOUND" & gFileSep & "SIM" & gFileSep & soundName
  347.   set buildingsConnected to 0
  348.   repeat while soundBusy(1)
  349.   end repeat
  350.   resetGame()
  351.   flushBuffer()
  352. end
  353.  
  354. on simTroubleAlert bmap
  355.   set the available of getProp(the simButtonList of gSimObject, #alert) to 1
  356.   set the locH of getProp(the simButtonList of gSimObject, #alert) to 131
  357.   set the locH of sprite 48 to 131
  358.   set the locV of getProp(the simButtonList of gSimObject, #alert) to 411
  359.   set the locV of sprite 48 to 411
  360.   set the bmap of getProp(the simButtonList of gSimObject, #alert) to bmap
  361.   set the castNum of sprite 48 to bmap
  362.   updateStage()
  363. end
  364.  
  365. on newGameAlert
  366.   set the castNum of sprite 48 to the number of cast "newGameAlert"
  367.   set the locH of sprite 48 to 320
  368.   set the locV of sprite 48 to 198
  369. end
  370.  
  371. on removeMessage
  372.   set the available of getProp(the simButtonList of gSimObject, #alert) to 0
  373.   set the locH of sprite 48 to -500
  374. end
  375.  
  376. on roadCongrats
  377.   set congratsName to "CONGRAT" & string(random(5))
  378.   sound playFile 1, gCDpath & "SOUND" & gFileSep & "SIM" & gFileSep & congratsName
  379.   repeat while soundBusy(1)
  380.   end repeat
  381. end
  382.  
  383. on redrawCell object, whichSprite
  384.   if the tileType of object <> #BG then
  385.     set the locH of sprite whichSprite to the Xloc of object
  386.     set the locV of sprite whichSprite to the Yloc of object
  387.     set the castNum of sprite whichSprite to the tileNum of object
  388.   end if
  389. end
  390.  
  391. on simIntroSong
  392.   sound playFile 1, gCDpath & "SOUND" & gFileSep & "SIM" & gFileSep & the scenarioSong of gSimObject
  393.   set the scenarioSong of gSimObject to #pending
  394. end
  395.  
  396. on unpuppetAllSprites
  397.   repeat with x = 1 to 48
  398.     puppetSprite(x, 0)
  399.   end repeat
  400. end
  401.  
  402. on errorTrapForDebugging moveList, pathList, availableMoveList
  403.   if count(moveList) = 0 then
  404.     put "pathList :" && pathList
  405.     put "availableMoveList :" && availableMoveList
  406.     redrawBG(gSimObject)
  407.     drawTruck(gTruckObject)
  408.     beep(2)
  409.     repeat while 1 = 1
  410.     end repeat
  411.   end if
  412. end
  413.  
  414. on putValues
  415.   repeat with x = 1 to count(the cellList of gSimObject)
  416.     set cellObj to getAt(the cellList of gSimObject, x)
  417.     showObjectProps(cellObj)
  418.   end repeat
  419. end
  420.